package com.foreknow.dao.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
    //添加信息
    @Override
    public boolean save(Guestbook guestbook) throws SQLException {
        int isRight = jdbcTemplate.update("insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?)",guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime());
        return isRight==1;
    }

    //根据id查询信息
    @Override
    public Guestbook queryById(Integer id) throws SQLException {
        EntityMapping entityMapping = mappingFactory.getMapping(mappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from guestbook where id = ?",entityMapping,id);
        if(list.size()>0){
            return (Guestbook)list.get(0);
        }
        return null;
    }

    //修改
    @Override
    public boolean update(Guestbook guestbook) throws SQLException {
        int isRight = jdbcTemplate.update("update guestbook set name = ?,sex = ?,age = ?,marry = ?,address=?,phone=? where id = ?",guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime(), guestbook.getId());
        return isRight==1;
    }

    //根据id删除
    @Override
    public boolean delete(Integer id) throws SQLException {
        int isRight = jdbcTemplate.update("delete from guestbook where id=?",id);
        return isRight==1;
    }

    //查询所有的信息
    @Override
    public List<Object> getQueryAll() throws SQLException {
        EntityMapping entityMapping = mappingFactory.getMapping(mappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from guestbook",entityMapping);
        return list;
    }
}
